/*
 * 功能：负责处理与课程相关的HTTP请求
 * 作者：张杨(Sunny)
 * 日期： 2024/11/10 下午2:21
 */
package com.example.springboot.control;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Course;
import com.example.springboot.service.CourseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 课程信息控制器，提供课程信息相关的CRUD（创建读取更新删除）操作
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    CourseService courseService;

    // 添加课程信息
    @PostMapping("/add")
    public Result add(@RequestBody Course course) {
        courseService.save(course);
        return Result.success();
    }

    // 更新课程信息
    @PutMapping("/update")
    public Result update(@RequestBody Course course) {
        courseService.updateById(course);
        return Result.success();
    }

    // 删除课程信息
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        courseService.removeById(id);
        return Result.success();
    }

    // 批量删除课程信息
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        courseService.removeBatchByIds(ids);
        return Result.success();
    }

    // 查询课程信息
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Course> courseList = courseService.list(new QueryWrapper<Course>().orderByDesc("name"));
        return Result.success(courseList);
    }

    // 分页查询课程信息，并支持根据多个条件进行筛选
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String name, // 课程名称
                               @RequestParam String description, // 课程描述
                               @RequestParam String type, // 课程类型
                               @RequestParam String time, // 上课时间
                               @RequestParam String place, // 课程地点
                               @RequestParam  String state) { // 课程状态
        Page<Course> page = courseService.selectByPage(pageNum, pageSize, name, description, type, time, place, state);
        return Result.success(page);
    }

    //导出信息到本地 Excel 文件
    @AuthAccess
    @GetMapping("/export")
    public void exportData(@RequestParam(required = false) String name,
                           @RequestParam(required = false) String coach,
                           @RequestParam(required = false) String ids,
                           HttpServletResponse response) throws IOException {
        courseService.exportData(name, coach, ids, response);
    }

    // 导入信息到数据库
    @AuthAccess
    @PostMapping("/import")
    public Result importData(@RequestParam("file") MultipartFile file) {
        try {
            return courseService.importData(file);
        } catch (IOException e) {
            return Result.error("导入失败");
        }
    }
}
